<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; utf-8">
    <title>模板配置</title>
    <%@ include file="../../static/top.jsp" %>

</head>
<body class="sticky-header">

<section> <!-- left side start-->
    <%@ include
            file="../../static/menu.jsp" %> <!-- left side end-->

    <!-- main content start-->
    <div class="main-content">

        <!-- header section start-->


        <%@ include file="../../static/header.jsp" %>

        <div class="wrapper">
 			<div class="page-heading">
				<ul class="breadcrumb">
					<li><a href="#">我的采集</a></li>
					<li class="active">模板配置</li>
				</ul>
			</div>
            <div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">模板配置</div>
                        <div class="panel-body">
                        <div class="col-md-12">

                                <form class="form-horizontal" role="form">
                                    <div class="form-group">

                                        <label for="username" class="col-sm-2 control-label">项目列表</label>

                                        <div class="col-sm-2 ">
                                            <select class="form-control" id="plist">
                                           	 <option value="" >请选择</option> 
												<c:forEach var="it"  items="${sessionScope._sessionkey.plist}"   varStatus="status" begin="0" end="${sessionScope._sessionkey.plist.size()}" step="1">
													 <option value="${it.id}" >${it.project_name}</option> 
							                         
							
							                    </c:forEach>
											</select>
                                        </div>
                                        <label for="username" class="col-sm-2 control-label">域名</label>
                                        <div class="col-sm-3 ">
                                        	<select class="form-control" id="website">
                                           	
											
											</select>
                                        </div>
                                        <div class="col-sm-1 ">
                                            <button id="searchBtn" type="button" class="btn btn-success">查询</button>
                                        </div>
                                       
                                        
                                    </div>

                                </form>

                            </div>
                            <div class="col-md-12">
                            <div class="col-md-12">

			                                <form class="form-horizontal" role="form">
			                                    <div class="form-group col-sm-10">
			                                        <button id="addTmplBtn" type="button"
			                                                class="  btn btn-info">添加模板
			                                        </button>
			
			                                    </div>
			
			                                </form>
			
			                            </div>
                                <table class="table table-bordered table-striped table-condensed">
                                    <thead>
                                    <tr>
                                 
                                        <th width="300px">模板名称</th>
                                        <th width="250px">采集地址</th>
                                        <th>创建时间</th>
                                        <th>操作</th>

                                    </tr>
                                    </thead>
                                    <tbody id="tbody">


                                    </tbody>
                                </table>
                                  <div class="paging">
									<ul class="pagination" id="Pagination">

									</ul>
								</div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

        </div>
        <!--body wrapper end-->
	<div tabindex="-1" role="dialog" id="createTemp"
             class="modal fade bs-example-modal-lg" style="display: none;">
            <div role="document" class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" data-dismiss="modal" aria-label="Close"
                                class="close">
                            <span aria-hidden="true">×</span>
                        </button>
                        <h4 class="modal-title">新建采集域</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                             <div class="form-group">
                                <label class="control-label col-sm-2">项目列表</label>
                                <div class="col-sm-10" >
                                    <select id="project_list" class="form-control">
                                    	<option value="" >请选择...</option> 
                                    	<c:forEach var="it"  items="${sessionScope._sessionkey.plist}"   varStatus="status" begin="0" end="${sessionScope._sessionkey.plist.size()}" step="1">
											<option value="${it.id}" >${it.project_name}</option> 
							             </c:forEach>
                                    
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-sm-2">采集域</label>
                                <div class="col-sm-10" >
                                    <select id="host_list" class="form-control">
                                    	
                                    
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-sm-2">模板名称</label>
                                <div class="col-sm-10" >
                                    <input type="text" class="form-control" id="tname">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-sm-2">网址</label>
                                <div class="col-sm-10" >
                                    <input type="text" class="form-control" id="url">
                                </div>
                            </div>
                          
                            
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-info" id="saveBtn">保存 </button>
                        <button type="button" class="btn btn-success" id="closeBtn" >关闭</button>
                    </div>
                </div>
            </div>
        </div>

        <!--footer section start-->
        <%@ include file="../../static/footer.jsp" %>
        <!--footer section end-->


    </div>
    <!-- main content end--> </section>

<script type="text/javascript">
var pageSize = 20, pageinit = true;	
var pid = ${pid};
var hid = ${hid};
    $(document).ready(function () {
    	$.ajaxSetup({    
    	    async : false    
    	});  
    	
    	if(pid){
    		$("#plist").val(pid);
    		
    		
    			$.get("/action/my/h/getAllHostList",{"pid":pid},function(data){
       			 var list = data.data;
       			 if(list){
       				 var op = " <option value='' >请选择</option> ";
       				 for(var i=0;i<list.length;i++){
       					if(list[i].id==hid){
       					 	op +="<option value="+list[i].id+" selected >"+list[i].website+"</option>" 
       			    	}else{
       			    	 	op +="<option value="+list[i].id+">"+list[i].website+"</option>" 
       			    	}
       					
       				 }
       				 $("#website").html(op);
       			 }
       		})
    		
    	}
    	
    	queryList(1, true);
    	
    	$("#searchBtn").click(function () {
    		
    		queryList(1, true);
    	 })
    	 $("#addTmplBtn").click(function () {
    		 
    		 $("#project_list").val("");
    		 $("#url").val("");
    		 $("#tname").val("");
    	     $("#createTemp").modal("show");
    	 })
    	 
    	  $("#closeBtn").click(function () {
    		 $("#project_list").val("");
     		 $("#url").val("");
     		 $("#tname").val("");
     	     $("#createTemp").modal("hide");
     	  })
     	  
     	 $("#saveBtn").click(function () {
     		var pid = $("#project_list").val();
     		var hid = $("#host_list").val();
     		var url = $("#url").val();
     		var tname = $("#tname").val();
     		
     		if(pid==""||pid==null){
     			layer.msg("请选择项目列表")
     			return;
     		}
     		if(hid==""||hid==null){
     			layer.msg("请选择采集域")
     			return;
     		}
     		if(url==""||url==null){
     			layer.msg("请填写网址或域名")
     			return;
     		}
     		if(tname==""||tname==null){
     			layer.msg("请填写模板名称")
     			return;
     		}
     		$.blockUI({ message: '<h1>请稍等...</h1> ', css: { width: '200px', height: '100px' } });
     		$.post("/action/my/m/addNewTmpl",{"pid":pid,"hid":hid,"url":url,"tname":tname},function(data){
     			$.unblockUI();
    			if(data.code==200){
    				layer.msg(data.msg);
    				$("#createTemp").modal("hide");
    				queryList(1, true);
    			}else{
    				layer.msg(data.msg)
    			}
   			})
     		
     		
     		
     	 })
     	 
       	$("#plist").change(function(){
        		var p = $(this).val();
        		if(p!=""&&p!=null){
        			$.get("/action/my/h/getAllHostList",{"pid":p},function(data){
           			 var list = data.data;
           			 if(list){
           				 var op = "<option value='' >请选择</option> ";
           				 for(var i=0;i<list.length;i++){
           					 op +="<option value="+list[i].id+">"+list[i].website+"</option>" 
           				 }
           				 $("#website").html(op);
           			 }
           		})
        		}else{
        			layer.msg("请选择项目列表")
        		}
        			
        		 
           });
     	 
     	$("#project_list").change(function(){
     		var p = $(this).val();
     		if(p!=""&&p!=null){
     			$.get("/action/my/h/getAllHostList",{"pid":p},function(data){
        			 var list = data.data;
        			 if(list){
        				 var op = "";
        				 for(var i=0;i<list.length;i++){
        					 op +="<option value="+list[i].id+">"+list[i].website+"</option>" 
        				 }
        				 $("#host_list").html(op);
        			 }
        		})
     		}else{
     			layer.msg("请选择项目列表")
     		}
     			
     		 
        });

    });
    
    function startJob(request_id){
    	
 		layer.confirm('确定开始启动任务？', {
 			  btn: ['确定','取消'] //按钮
 			}, function(){
 				//$.blockUI({ message: '<h1>请稍等...</h1> ', css: { width: '200px', height: '100px' } });
 				$.post("/action/my/url/startUrl",{"request_id":request_id,"status":-1},function(data){
 					//$.unblockUI();
 					if(data.code==200){
 						layer.msg("操作成功");
 					
 					}else{
 						layer.msg(data.msg)
 					}
 				})
 				
 			});
    }
    
	
    

    function queryList(page, pageinit) {
	    $("#tbody").empty();
	        
	        
	     var p = $("#plist").val();
	     var url = $("#website").val();
	  
	   	 $.get("/action/my/m/getTemplateList",{"pageNum" : page,"pageSize" : pageSize,"pid":p,"hid":url},function(data){
	   		    var  content;
	   			var _totalPage = data.maxPage;
	   			if (data.data.length) {
	   				for (var i = 0; i < data.data.length; i++) {
	   					
	   					var contain = data.data[i];	
	   					content += "<tr>" ;
	   					content += "<td  >" + contain.template_name + "</td>" ;
	   					content += "<td>" + contain.url + "</td>" ;
	   					content += "<td>" + contain.create_time+ "</td>" ;
	   					if(contain.prid){
	   						content += "<td><a href='/portal/rid/" + contain.request_id + "'>查看模板</a>|<a href='javascript:void(0)' onclick=\"showJob('"+contain.request_id+"')\">查看任务</a></td></tr>" ;
	   						
	   					}else{
	   						content += "<td><a href='/portal/rid/" + contain.request_id + "'>查看模板</a>|<a href='javascript:void(0)' onclick=\"showJob('"+contain.request_id+"')\">查看任务</a>|<a href='javascript:void(0)' onclick=\"startJob('"+contain.request_id+"')\">启动下载任务</a></td></tr>" ;
	   						
	   					}
	   					
	   				}
	   				$("#tbody").append(content);
	   			}
	   			if (pageinit) {
	   				$('#Pagination').empty();
	   				$.jqPaginator('#Pagination',{
	   					totalPages : _totalPage,
	   					visiblePages : 10,
	   					currentPage : 1,
	   					prev : '<li class="prev"><a href="javascript:scroll(0,0)">上页</a></li>',
	   					next : '<li class="next"><a href="javascript:scroll(0,0)">下页</a></li>',
	   					first : '<li class="first"><a href="javascript:scroll(0,0)">首页</a></li>',
	   					last : '<li class="last"><a href="javascript:scroll(0,0)">尾页</a></li>',
	   					page : '<li class="page"><a href="javascript:scroll(0,0)">{{page}}</a></li>',
	   					onPageChange : function(num, type) {
	   						if ((this.currentPage != 1)|| (1 != num)) {
	   							queryList(num,false);
	   						}
	   					}
	   				});
	   			}
	   	  })
        

       
  


    }
    
    function showJob(request_id){
    	 window.location.href = "/action/my/m/toMyJob?request_id="+request_id+"&mid=a1&aid=ch12";
    	 
    	 
    }
    
 

</script>

</body>
</html>
